package com.itheima.advice;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

//@Aspect
@Slf4j
//@Component
public class RecordTimeAdvice {
//    @Around("execution(* com.itheima.service.impl.*.*(..))")
    public Object abc(ProceedingJoinPoint pjp){
        Long begin = System.currentTimeMillis();
        MethodSignature signature = (MethodSignature) pjp.getSignature();
        Object object = null;
        try {
            object = pjp.proceed();
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
        Long end = System.currentTimeMillis();
        log.info(signature.getMethod().getName() + "方法执行的时间是" + (end - begin) + "毫秒");
        return object;
    }

    @Pointcut("execution(* com.itheima.service.impl.*.*(..))")
    public void pt(){

    }


    @Before("pt()")
    //执行原方法之前
    public void before(){
        log.info("执行了before方法.................执行原方法之前");
    }

    @AfterReturning("pt()")
    //正常执行原方法后
    public void afterReturning(){
        log.info("执行了afterReturning方法.................正常执行原方法后");
    }

    @AfterThrowing("pt()")
    //执行原方法出现异常后
    public void afterThrowing(){
        log.info("执行了afterThrowing方法.................执行原方法出现异常后");
    }

    @After("pt()")
    //最终要执行的方法 无论是否出现异常
    public void after(){
        log.info("执行了after方法.................最终要执行的方法");
    }
}
